
import Vue from 'vue'
import Vuex from 'vuex'
// import {get, post} from "../api/index";
import { getuser,adduser,deluser,updateuser} from '../api/user';
import {gettrend,addtrend,deltrend,updatetrend} from '../api/trend';

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    totableData:[],
    totrendData:[],
    name:""
    
  },
  mutations: {
    vgetusers(state,payload){
        state.totableData=payload.ok
    },
    vadduser(state,payload){
      // state.totableData=payload.data
    },
    vupdatauser(state,payload){

    },
    vselectuser(state,payload){
      state.totableData=payload
    },
    getname(state,payload){
      state.name=payload
    },




    gettrend(state,payload){
      state.totrendData=payload
    },
    vselecttrend(state,payload){
      state.totrendData=payload
    },
    vaddtrend(state,payload){
      
    },
    vupdatetrend(state,payload){
      
    }

  },
  actions: {
    //获取所有数据
      vgetuser(context){
        getuser()
          .then((item,index)=> {context.commit("vgetusers",{
            ok:item,
          })
        })
      },
      async gettrend(context){
        let res=await gettrend()
        context.commit("gettrend",res)
      },
      //增加
      vadduser(context,add){
        adduser(add)
        .then((data)=>{context.commit("vadduser",{
          data:[data]
          })
      })
      },
     async addtrend(context,add){
        await addtrend(add)
        context.commit("vaddtrend")
      },
      //删除
     vdeluser(context,del){
         deluser(del)
          .then((data)=>{context.commit("vdeluser"),{
            data:data
          }})
      },
     async deltrend(context,del){
        await deltrend(del)
        context.commit("deltrend")
      },
      //更新
      vupdatauser(context,payload){
        console.log(payload.id)
        console.log(payload.data)
        updateuser(payload.id,payload.data)
        .then((datas)=>{context.commit("vupdatauser"),{
          data:datas
        }})
      },
      async updatetrend(context,update){
        await updatetrend(update.id,update.data)
        context.commit("vupdatetrend")
      }, 
   //查询
        vselectuser(context,payload){
          getuser(payload)
          .then((item)=>{
      //context.commit("vselectuser")
            console.log("item",item)
    })
  },
       async selecttrend(context,payload){
          console.log(payload)
          let data= await gettrend(payload)
          context.commit("vselecttrend",data)
  }
},
  modules: {

  }
})
